<?php
namespace yan\sign_google\factory;

class FetchAccessToken extends \yan\sign_google\AbstractFactory{
	public $environment;
	public $curl;
	private $token;
	private $uri;
	function build($environment, $curl)
    {
        $this->environment = $environment;
        $this->curl = $curl;
        $this->uri = 'https://oauth2.googleapis.com/token';
        $this->init();
        return $this;
    }
    public function init(){

    }
	public function run($data=[]){
		$post_data = [
			'code'=>$data['code'],
			'client_id'=>$this->environment->getClientId(),
			'client_secret'=>$this->environment->getClientSecret(),
			'redirect_uri'=>$data['redirect_uri'],
			'grant_type'=>'authorization_code',
		];
		$headers = [];
		$headers[] = 'Content-Type: application/x-www-form-urlencoded';
		$return_data = $this->curl->build($this->uri)->setHeader()->setPostData($post_data)->run();
		return $return_data;
	}
	
}